local juesi = fk.CreateSkill{
  name = "zy__juesi",
}
local juesi_ = {
  name = "zy__juesi",
  anim_type = "offensive",
  card_num = 1,
  min_target_num = 1,
  can_use = function(self, player)
    return not player:isNude() and player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 
  end,
  card_filter = function(self, player, to_select, selected)
    return #selected == 0 and not player:prohibitDiscard(Fk:getCardById(to_select))
  end,
  target_filter = function(self, player, to_select, selected)
    local target = Fk:currentRoom():getPlayerById(to_select)
    return player:inMyAttackRange(target) and not target:isNude()
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local cards = Fk:getCardById(effect.cards[1])
    local targets = {}
    local cards_get = {}
    local dummy = Fk:cloneCard("dilu")
    room:setPlayerMark(player, "juesi_mind", 1)
    if room:getCardArea(effect.cards[1]) == Card.DiscardPile then
      dummy:addSubcard(effect.cards[1])
    end
    for _, seat in ipairs(effect.tos) do
      table.insert(targets, seat)
    end
    room:throwCard(effect.cards, self.name, player, player)
    room:sortPlayersByAction(targets)
    for _, id in ipairs(targets) do
      if player.dead then return end
      local p = room:getPlayerById(id)
      if not p.dead then
        local choice = room:askForChoice(p, {"discard_choice", "duel_choise"}, self.name, nil, false, {"discard_choice", "duel_choise"})
        if choice == "discard_choice" and not p.dead and not p:isNude() then 
          local others_discard = room:askForDiscard(p, 1, 1, true, self.name, false)
        elseif choice == "duel_choise" and not p.dead then
          room:useVirtualCard("duel", nil, player, p, self.name, true)
        end
      end
    end
    local view = table.filter(player.room.discard_pile, function(id)
      return Fk:getCardById(id):getMark("juesi_slash_record-turn") > 0
    end)
    local get = {}
    if #view > 0 then
      for _, idc in ipairs(view) do
        table.insert(get, idc)
        room:setCardMark(Fk:getCardById(idc), "@@juesi_slash-turn", 1)
      end
    end
    if #get > 0 then 
      dummy:addSubcards(get)
    end
    if #dummy.subcards > 0 then 
      room:obtainCard(player.id, dummy, true, fk.ReasonJustMove)
    end
    room:setPlayerMark(player, "juesi_mind", 0)
  end,
}
local juesi_record = {
  mute = true,
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if player:getMark("juesi_mind") < 1 then return false end
    if player:hasSkill(self) and not player.dead then return true
    end
  end,
  on_cost = function(self, event, target, player, data)
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, move in ipairs(data) do
      for _, info in ipairs(move.moveInfo) do
        local id = info.cardId
        local card = Fk:getCardById(id)
        if move.toArea == Card.DiscardPile and card.trueName == "slash" then
          room:setCardMark(card, "juesi_slash_record-turn", 1)
        end
      end
    end
  end,
}
local juesi_targetmod = {
  name = "#juesi_targetmod",
  bypass_times = function(self, player, skill, scope, card, to)
    return card and card.trueName == "slash" and not card:isVirtual() and card:getMark("@@juesi_slash-turn") > 0
  end,
}
juesi:addEffect("active", juesi_)
juesi:addEffect(fk.AfterCardsMove, juesi_record)
juesi:addEffect("targetmod", juesi_targetmod)
return juesi